package com.atguigu.gulimall.order.listener;


import com.atguigu.common.to.mq.SecKillOrderTo;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * <p>Title: OrderSecKillListener</p>
 * Description：
 * date：2020/7/9 20:23
 */
@Slf4j
@RabbitListener(queues = "order.seckill.order.queue")
@Component
public class OrderSecKillListener {


	@Autowired
    private OrderService orderService;
	@RabbitHandler
	public void listener(SecKillOrderTo secKillOrderTo, Channel channel, Message message) throws IOException {

		//System.out.println(orderEntity.getOrderSn()+"收到过期的消息，关闭订单"+orderEntity.getOrderSn());
		try {
			log.info("收到秒杀订单的信息，准备创建订单，订单信息：{}",secKillOrderTo);
			//orderService.closeOrder(orderEntity);
			orderService.createSecKillOrder(secKillOrderTo);
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		}catch (Exception e){
			channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
		}


	}
}
